// pages/config/config.js
import {storage} from "../../utils/storage";
const app = getApp();

// 配置页面，配置安全距离以及安全电量
Page({
  /**
   * 页面的初始数据
   */
  data: {
      Distance: storage.get('Distance') ||'1000',
      Capacity: storage.get('Capacity') ||'100',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
      
  },

  onShow: function() {
    this.setData({
        Distance: storage.get('Distance') ||'1000',
        Capacity: storage.get('Capacity') ||'100',
    });
  },

  handleDistance(e) {
      this.setData({
        Distance: e.detail.value
      });
  },

  handleCapacity(e) {
      this.setData({
        Capacity: e.detail.value
      });
  },

  submit() {
      if (!this.data.Distance){
          wx.showToast({ title: '请填写安全距离', icon: 'none' });
          return;
      }
      if (!this.data.Capacity){
          wx.showToast({ title: '请填写安全电量', icon: 'none' });
          return;
      }
      
      console.log('submit') 
      storage.setSync('Distance', this.data.Distance);
      storage.setSync('Capacity', this.data.Capacity);

      app.Config.Distance = this.data.Distance;
      app.Config.Capacity = this.data.Capacity;
      
      wx.showToast({ title: '保存成功', icon: 'none' });

      setTimeout(() => {
          wx.navigateBack({
              delta: 1
          });
      },1500);
  }

});
